<?php
namespace app\index\controller;


use app\index\model\Avatar;
use app\index\model\Users;
use PHPMailer\SendEmail;
use think\auth\Auth;
use think\Controller;
use think\Cookie;
use think\Db;
use think\Loader;
use think\Request;
use think\Session;

class Index extends Controller
{
    function __construct(Request $request)
    {
        parent::__construct($request);
        Session::init();
        if(Session::get('name')!=1){
            $this->error('请先登录',url('index/Login/login','',''));
        }
    }
    public function index()
    {
        //权限
        $uid = Session::get('id');

        $auth = new Auth();
        $rule = $this->request->module() . '/' . $this->request->controller() . '/' . $this->request->action();

        $result = $auth->check($rule, $uid);

            return $this->fetch();
    }
    public function info()
    {
        \think\Config::set('url_common_param', true);
        $request=Request::instance();
        $name=$request->get('username','','trim');
        $nickname=$request->get('nickname','','trim');
        $vip=$request->get('vip/a',[],'trim');
        $birthday1=$request->get('birthday1','','trim');
        $birthday2=$request->get('birthday2','','trim');
        $reg_time1=$request->get('reg_time1','','trim');
        $reg_time2=$request->get('reg_time2','','trim');
        $sex=$request->get('sex','','trim');
        $pic_url=$request->get('pic_url/a',[],'trim');



        $data=[];
        //用户名
        if(''!==$name){
            $data['username']=$name;
        }
        $this->assign('username',$name);
        //昵称
        if(''!==$nickname){
            $data['nickname']=$nickname;
        }
        $this->assign('nickname',$nickname);
        //vip
        if(3!==count($vip) and []!==$vip){
            $data['vip']=$vip;
        }
        $this->assign('vip',$vip);
        //生日
        if(''!==$birthday1){
            $data['birthday1']=$birthday1;
        }
        $this->assign('birthday1',$birthday1);
        if(''!==$birthday2){
            $data['birthday2']=$birthday2;
        }
        $this->assign('birthday2',$birthday2);
        //注册时间
        if(''!==$reg_time1){
            $data['reg_time1']=$reg_time1;
        }
        $this->assign('reg_time1',$reg_time1);
        if(''!==$reg_time2){
            $data['reg_time2']=$reg_time2;
        }
        $this->assign('reg_time2',$reg_time2);
        //性别
        if($sex!=3 and ''!=$sex){
            $data['sex']=$sex;
        }
        $this->assign('sex',$sex);
        //电话




        //邮箱



        //头像
        if(''!=$pic_url and 2!=count($pic_url)){
            $data['pic_url']=$pic_url;
        }
        $this->assign('pic_url',$pic_url);






        //点击排序

        $get = $request->get();

        $sort = $request->get('sort', '', '');
        if ('asc' == $sort) {
            $data['order'] = 'desc';
            $get['sort'] = 'desc';
            $this->assign('flag', '↑');
        } elseif ('desc' == $sort) {
            $data['order'] = 'asc';
            $get['sort'] = '';
            $this->assign('flag', '↓');
        } else {
            $data['order'] = '';
            $get['sort'] = 'asc';

        }


        $list=Users::userList($data,$request->get());
        $this->assign('get', $get);

        $pagee=$list->lastPage();

        $this->assign('pagee',$pagee);

        $this->assign('list',$list);

        return $this->fetch();
    }
    public function bottom()
    {
        return $this->fetch();
    }
    public function menubar()
    {
        return $this->fetch();
    }
    public function top()
    {

        $username=Session::get('username');
        $this->assign('username',$username);
        return $this->fetch();
    }
    public function main()
    {
        return $this->fetch();
    }

    public function huishou()
    {
        Session::init();
        if(Session::get('name')!=1){
            $this->error('请先登录',url('index/Login/login','',''));
        }
        //权限
        $uid = Session::get('id');

        $auth = new Auth();
        $rule = $this->request->module() . '/' . $this->request->controller() . '/' . $this->request->action();

        $result = $auth->check($rule, $uid);
        if($result==false){
            $this->error('没有访问权限','index/Index/info');
        }
        $list=Users::huishou();
        $this->assign('list',$list);





        return $this->fetch();
    }
    public function del(){
        \think\Config::set('url_common_param', true);
        $id = $this->request->param('id');
//        $pagee= $this->request->param('pagee');
        $g=input('get.');

//        echo "<pre>";
//        print_r($g);
//        echo "</pre>";
//        exit();

        if($this->request->isGet() && !empty($id)){

            $ret = \app\index\model\Users::destroy($id);
            if($ret){

                if(!empty($g['0'])){
                    $ga=$g['0'];
                }else{
                    $ga='';
                }


//                $page2=Users::userList()->lastPage();
//                if($pagee<$page2){

                    $this->success('删除成功',url('index/Index/info',$ga));
//                }else{
//                    $this->success('删除成功',url('index/Index/info'));
                }

            else{
                $this->error('删除失败');
            }
        }}

//物理删除
    public function dell(){

        $id = $this->request->param('id');
        $p=Avatar::get($id);
        $yuan=$p['pic_url'];
        $suolue=$p['thumb_url'];
        if($this->request->isGet() && !empty($id)){

            $ret = \app\index\model\Users::destroy($id,true);
            $tu=\app\index\model\Avatar::destroy($id,true);
            if($ret and $tu){
                unlink($yuan);
                unlink($suolue);
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }
    }

    public function huifu(){
        $id=Request::instance()->param('id');
        if($id){
            $up=Db::table('app_users')->where('id',$id)->update(['del_time'=>Null]);
            if($up){
                $this->success('恢复成功');
            }else{
                $this->error('恢复失败');
            }
        }else{
            $this->error('没有id值');
        }
    }




    public function clear(){
        Session::init();
        Session::destroy();
        $this->success('退出登录,再见!!!',url('index/Login/logink','',''));

    }
    public function clearS(){
        Cookie::init();
        Cookie::clear();
        Cookie::delete('usera');
        $this->success('退出30天免登录成功',url('index/Login/login','',''));

    }

    public function email() {
        $toemail='659804793@qq.com';
        $name='static7';
        $subject='QQ邮件发送测试';
        $content='http://xmq15.com/index.php/index/Login/repsw';
        dump(send_mail($toemail,$name,$subject,$content));
    }

    public function emaill()
    {
        $rt = SendEmail::SendEmail();
        if ($rt) {
            $this->success('邮件发送成功', 'index/Login/login');
        } else {
            $this->error('邮件发送失败');
        }
    }
    //手动引入email类
//    public function shouemail(){
//        Loader::import('PHPMailer.class#smtp');
//        Loader::import('PHPMailer.PHPMailer');
//        Loader::import('PHPMailer.SendEmail');
//
//        $rt = SendEmail::SendEmail();
//        if ($rt) {
//            $this->success('邮件发送成功', 'index/Login/login');
//        } else {
//            $this->error('邮件发送失败');
//        }
//    }


public function ttt(){


    echo 123;
}
}
